# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://docs.scrapy.org/en/latest/topics/item-pipeline.html


# useful for handling different item types with a single interface
from itemadapter import ItemAdapter
import pymysql
from scrapy.pipelines.images import ImagesPipeline
from scrapy import Request

class DangdangPipeline:
    def process_item(self, item, spider):
        return item

class MysqlPipeline(object):
      def __init__(self,host,user,password,database,port):
        self.host = host
        self.user = user
        self.password = password
        self.database = database
        self.port = port
      @classmethod
      def from_crawler(cls,crawler):
      	return cls(
              host = crawler.settings.get("MYSQL_HOST"),
              database = crawler.settings.get("MYSQL_DATABASE"),
              user = crawler.settings.get("MYSQL_USER"),
              password = crawler.settings.get("MYSQL_PASS"),
              port  = crawler.settings.get("MYSQL_PORT")
            )

      '''数据库的连接'''
      def open_spider(self, spider):
        print('='*70)
        self.db = pymysql.connect(self.host,self.database,self.user,self.password,self.port,charset="utf8")
        print('='*70)
        self.cursor = self.db.cursor()
        return item

      """docstring for ClassName"""
      def process_item(self, item, spider):
        sql = "insert into db(pic,name,author,price,time) values('%s','%s','%s','%s','%s','%s')"%(item['pic'],item['name'],item['author'],item['price'],item['time'])
        self.cursor.execute(sql)
        self.db.commit()
        return item

      def close_spider(self, spider):
        self.db.close()

class ImagePipeline(ImagesPipeline):
      def get_media_requests(self,item,info):
        yield Request(item['pic'])

      def file_path(self,request,response=None,info=None):
        url = request.url
        file_name = url.split("/")[-1]
        print(file_name)
        return file_name


      def item_completed(self,results,item,info):
        image_paths = [x['path'] for ok,x in results if ok]
        return item
